package com.rykj.qxj.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.rykj.qxj.interceptor.AccessLimitInterceptor;

import lombok.extern.log4j.Log4j;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

   // private final long MAX_AGE_SECS = 3600;
    
    @Value("${file-upload-path}")
	private String imagePath;
    @Value("${hostUrl}")
    private String hostUrl;
    
    @Autowired
	private AccessLimitInterceptor accessLimitInterceptor;


   /* @Override
    public void addCorsMappings(CorsRegistry registry) {
    	log.info("CorsRegistry   run ====================");
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(MAX_AGE_SECS);
    }*/
    /**
     * 
     * @Description: 添加资源映射        
     * @date:   2019年3月29日 下午3:11:01  
     * @author: wangshunyi
     */
    @Override  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        registry.addResourceHandler("/upload/**").addResourceLocations("file:///"+imagePath);  
    }  
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
    	//addPathPatterns("/**")  拦截的路径;    excludePathPatterns("/login")  不拦截的路径
        registry.addInterceptor(accessLimitInterceptor).addPathPatterns("/data-api/**");
    }
    @Bean
    public FilterRegistrationBean testFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CorsFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("hostUrl", hostUrl);
        registrationBean.setName("CorsFilter");
        registrationBean.setOrder(1);

        return registrationBean;
    }
}
